
//% color="#ff9f06" iconWidth=50 iconHeight=40
namespace gravityasr {

    //% block="ASR begin mode [MODE] micMode [MICMODE]" blockType="command"
    //% MODE.shadow="dropdown" MODE.options="MODE" 
    //% MICMODE.shadow="dropdown" MICMODE.options="MICMODE" 
    export function begin(parameter: any, block: any) {
        let mode = parameter.MODE.code;
        let micmode = parameter.MICMODE.code;
        Generator.addInclude("includeDFRobot_ASR","#include<DFRobot_ASR_M.h>");
        Generator.addObject("asrobject","DFRobot_ASR","asr");
        Generator.addCode(`asr.begin(${mode},${micmode});`);
 
    }
    
    //% externalFunc
    export function validateStr_(str) {
        let regx: RegExp =  /^[a-zA-Z\s]+$/;  
        if (!regx.test(str)) {
            return null;
        }
        return str;
    }

    //% block="ASR add command [COMMAND] number [NUMBER]" blockType="command"
    //% COMMAND.shadow="string" COMMAND.defl="kai deng"  COMMAND.params.validateFunc="validateStr_"
    //% NUMBER.shadow="number" NUMBER.defl="1" 
    export function addCommand(parameter: any, block: any) {
        let command = parameter.COMMAND.code;
        let num = parameter.NUMBER.code;

        Generator.addCode(`asr.addCommand(${command},${num});`);
 
    }
    //% block="ASR start" blockType="command"
    export function start() {

        Generator.addCode("asr.start();");
 
    }
       //% block="ASR read" blockType="reporter"
       export function read() {

        Generator.addCode(["asr.read()",Generator.ORDER_UNARY_POSTFIX]);
 
    } 

}
